<template>
  <div class="app-container">
    <Search @resetQuery="resetQuery" @handleQuery="handleQuery" :queryParams="queryParams" :showSearch="showSearch" />

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['basic:goodsCategory:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="Sort"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <List @handleDelete="handleDelete" @handleUpdate="handleUpdate" @handleSelectionChange="handleSelectionChange" @handleAdd="handleAdd" :loading="loading" :goodsCategoryList="goodsCategoryList" :isExpandAll="isExpandAll" />

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品分类对话框dialog -->
    <Dialog ref="goodsCategoryDialog" @submitForm="submitForm" @cancelBox="cancel" :formInfo="form" :goodsCategoryOptions="goodsCategoryOptions" :rules="rules" :title="title" :open="open" />
  </div>
</template>

<script setup name="GoodsCategory">
import { listGoodsCategory, getGoodsCategory, delGoodsCategory, addGoodsCategory, updateGoodsCategory } from "@/api/basic/goodsCategory";
import Dialog from "@/components/GoodsCategory/Dialog.vue";
import Search from '@/components/GoodsCategory/Search.vue';
import List from '@/components/GoodsCategory/List.vue';
import { ref, reactive, toRefs, nextTick } from 'vue';

const goodsCategoryList = ref([]);
const goodsCategoryOptions = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const isExpandAll = ref(true);
const refreshTable = ref(true);
const goodsCategoryDialog = ref(null); // 添加 ref 引用

const data = reactive({
  form: {
    id: null,
    name: null,
    parentId: null,
    isDelete: null,
    createTime: null
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    parentId: null,
    createTime: null
  },
  rules: {
    name: [
      { required: true, message: "分类名称不能为空", trigger: "blur" }
    ],
  },
  title: "" // 初始化 title
});

const { queryParams, form, rules, title } = toRefs(data);

/** 查询商品分类列表 */
function getList() {
  loading.value = true;
  listGoodsCategory(queryParams.value).then(response => {
    goodsCategoryList.value = response.data;
    loading.value = false;
  });
}

/** 查询商品分类下拉树结构 */
function getTreeselect() {
  listGoodsCategory().then(response => {
    goodsCategoryOptions.value = [];
    const data = { id: 0, name: '顶级节点', children: [] };
    data.children = response.data;
    goodsCategoryOptions.value.push(data);
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    parentId: null,
    isDelete: null,
    createTime: null
  };
  if (goodsCategoryDialog.value) {
    goodsCategoryDialog.value.resetForm();
  }
}

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  if (goodsCategoryDialog.value) {
    goodsCategoryDialog.value.resetForm();
  }
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd(row) {
  reset();
  getTreeselect();
  if (row != null && row.id) {
    form.value.parentId = row.id;
  } else {
    form.value.parentId = 0;
  }
  open.value = true;
  title.value = "添加商品分类";
}

/** 展开/折叠操作 */
function toggleExpandAll() {
  refreshTable.value = false;
  isExpandAll.value = !isExpandAll.value;
  nextTick(() => {
    refreshTable.value = true;
  });
}

/** 修改按钮操作 */
async function handleUpdate(row) {
  reset();
  await getTreeselect();
  if (row != null) {
    form.value.parentId = row.parentId;
  }
  getGoodsCategory(row.id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改商品分类";
  });
}

/** 提交按钮 */
function submitForm() {
  if (goodsCategoryDialog.value) {
    goodsCategoryDialog.value.submitForm();
  }
}

/** 删除按钮操作 */
function handleDelete(row) {
  this.$confirm('是否确认删除商品分类编号为"' + row.id + '"的数据项？').then(function() {
    return delGoodsCategory(row.id);
  }).then(() => {
    getList();
    this.$message.success("删除成功");
  }).catch(() => {});
}

getList();
</script>